package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 部门添加方法
     * @param department
     * @return
     */
    //@RequestMapping(value = "/save",method = RequestMethod.PUT)
    //RequestBody接收一个对象
    @PutMapping
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"添加失败" +e.getMessage());
        }
    }

    /**
     * 部门修改
     * @param department
     * @return
     */
    //RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Department department){
        try {
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败" +e.getMessage());
        }
    }
    /**
     * 部门删除
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败" +e.getMessage());
        }
    }
    /**
     * 部门批量删除
     * @param ids
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/del")//批量删除用数组接收参数
    public AjaxResult delete(@RequestBody Long[] ids){
        try {
            //调用批量删除方法
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!网络繁忙！" +e.getMessage());
        }
    }

    /**
     * 根据条件查询所有部门
     * @return
     */

    //@RequestMapping(value = "/loadAll",method = RequestMethod.PATCH)
    @PatchMapping
    public PageList<Department> loadPageByQuery(@RequestBody DepartmentQuery query){

        return departmentService.loadPageByQuery(query);
    }

    /**
     *  根据id查询部门
     * @param id
     * @return
     */
    //@RequestMapping(value = "/loadById",method = RequestMethod.GET)
    @GetMapping("{id}")
    public Department loadById(@PathVariable("id") Long id){
       return departmentService.loadById(id);
    }

    /**
     * 查询所有集合
     * @return
     */
    @PatchMapping("/loadAllDepartmentForm")
    public List<Department> loadAllDepartmentForm(){
        return departmentService.loadAll();
    }
    /**
     * 查询所有部门经理
     * loadAllEmployeeForm
     */
    @PatchMapping("/loadAllEmployeeForm")
    public List<Employee> loadAllEmployeeForm(){
        return departmentService.loadAllEmployeeForm();
    }


}
